package global.com.loansapplication.pay;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.alipay.sdk.app.PayTask;

import java.util.Map;

import global.com.loansapplication.base.BaseBiz;
import global.com.loansapplication.comm.MyApplication;
import global.com.loansapplication.pay.alipay.PayResult;
import global.com.loansapplication.ui.actviity.user.biz.RechargeBiz;
import global.com.loansapplication.util.LogUtils;


public class AliPayManager implements IPayManager {


    public static final String APPID = "2017112900243148";



    private static final int SDK_PAY_FLAG = 1;

    private PayCallback payCallback;

    private Activity activity;
    RechargeBiz rechargeBiz;


    public AliPayManager(Activity activity){
        this.activity = activity;
        rechargeBiz=new RechargeBiz();
    }

    public void payV2(final String orderInfo, final Activity activity) {

        this.activity = activity;
//        boolean rsa2 = (RSA2_PRIVATE.length() > 0);
//        Map<String, String> params = OrderInfoUtil2_0.buildOrderParamMap(APPID, rsa2);
//        String orderParam = OrderInfoUtil2_0.buildOrderParam(params);
//
//        String privateKey = rsa2 ? RSA2_PRIVATE : RSA_PRIVATE;
//        String sign = OrderInfoUtil2_0.getSign(params, privateKey, rsa2);
//        final String orderInfo = orderParam + "&" + sign;

        Runnable payRunnable = new Runnable() {

            @Override
            public void run() {
                PayTask alipay = new PayTask(activity);
       //         Map<String, String> result = alipay.payV2(orderInfo, true);
                Map<String, String> result = alipay.payV2(orderInfo, true);
                Log.i("msp", result.toString());

                Message msg = new Message();
                msg.what = SDK_PAY_FLAG;
                msg.obj = result;
                mHandler.sendMessage(msg);
            }
        };

        Thread payThread = new Thread(payRunnable);
        payThread.start();
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);

                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();

                    Log.e("result",resultStatus);
                    if (TextUtils.equals(resultStatus, "9000")) {

//                        showToast(activity, R.string.ali_pay_success);
                        if (payCallback!=null) {
                            payCallback.onSuccess();
                        }
                    } else {

//                        showToast(activity, R.string.ali_pay_fail);
                        if (payCallback!=null) {
                            payCallback.onFail();
                        }
                    }
                    break;
                }
                default:
                    break;
            }
        };
    };



    @Override
    public void recharge(String rechargeSettingId, final String payMethod, PayCallback payCallback) {
        this.payCallback = payCallback;
        rechargeBiz.recharge(MyApplication.userInfo.getUserCode(),rechargeSettingId,payMethod,new BaseBiz.Callback<String>() {
            @Override
            public void onSuccess(String o) {
                String orderInfo = o;
                payV2(orderInfo,activity);
            }

            @Override
            public void onFailure(String o) {
                LogUtils.d("2err:"+o);
            }
        });

    }



}
